/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.pay.service.impl;

import top.dzygod.modules.pay.domain.Class;
import top.dzygod.utils.ValidationUtil;
import top.dzygod.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import top.dzygod.modules.pay.repository.ClassRepository;
import top.dzygod.modules.pay.service.ClassService;
import top.dzygod.modules.pay.service.dto.ClassDto;
import top.dzygod.modules.pay.service.dto.ClassQueryCriteria;
import top.dzygod.modules.pay.service.mapstruct.ClassMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import top.dzygod.utils.PageUtil;
import top.dzygod.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzygod
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class ClassServiceImpl implements ClassService {

    private final ClassRepository classRepository;
    private final ClassMapper classMapper;

    @Override
    public Map<String,Object> queryAll(ClassQueryCriteria criteria, Pageable pageable){
        Page<Class> page = classRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(classMapper::toDto));
    }

    @Override
    public List<ClassDto> queryAll(ClassQueryCriteria criteria){
        return classMapper.toDto(classRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ClassDto findById(Long classId) {
        Class clazz = classRepository.findById(classId).orElseGet(Class::new);
        ValidationUtil.isNull(clazz.getId(),"Class","classId",classId);
        return classMapper.toDto(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClassDto create(Class resources) {
        return classMapper.toDto(classRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Class resources) {
        Class clazz = classRepository.findById(resources.getId()).orElseGet(Class::new);
        ValidationUtil.isNull(clazz.getId(),"Class","id",resources.getId());
        clazz.copy(resources);
        classRepository.save(clazz);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long classId : ids) {
            classRepository.deleteById(classId);
        }
    }

    @Override
    public void download(List<ClassDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ClassDto clazz : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("班级名称", clazz.getName());
            map.put("创建时间", clazz.getGmtCreate());
            map.put("修改时间", clazz.getGmtModified());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
